<template>
  <div class="Login" ref='IndexRef'>
    <div class="Login-banner"  @click="falseShow">
      <img src="static/img/Login/login.png" width="100%" height="auto" />
    </div>
    <div class="Login-Input" @click="falseShow">
      <form action="" method="post">
        <div class="Login-Input-User">
          <div class="Login-Input-User-Icon">
            <img src="static/img/Login/account.png" class="LoginImg" />
            <input class="UserInput" type="text" name="UserInput" id="user" value="" placeholder="请输入您的账号" v-model="User"
              autocomplete="off" @blur="userBlur" />
            <img src="static/img/Login/close.png" class="UserInput-Icon" @click="ClearUser" />
          </div>
        </div>
        <div class="Login-Input-Pwd">
          <div class="Login-Input-Pwd-Icon">
            <img src="static/img/Login/password.png" class="LoginImg" />
            <input class="PwdInput" type="password" name="password" id="pwd" value="" placeholder="请输入您的密码"
              autocomplete='off' v-model="Pwd" @blur="pwdBlur" />
          </div>
        </div>
        <div class="Login-Input-Select">
          <div class="Input-Select">
            <input type="text" placeholder="请选择幼儿园" @click.stop="searSchool" @blur="pwdBlur" @input="schoolIpt" v-model="value">
          </div>
        </div>
      </form>
    </div>
    <div class="Login-Info"  @click="falseShow">
      <button class="Login-Info-Button" @click="login(1)">登录</button>
    </div>
    <div class="Login-ForgetPwd"  @click="falseShow">
      <span @click="GotoForgetPwd()">忘记密码?</span>
    </div>
    <div class="Login-InitialPwd"  @click="falseShow">
      <span @click="GotoInitialPwd()">为了账户安全,建议 <span>更改初始密码</span></span>
    </div>

    <div class="school-item" v-show="schoolShow">
      <template v-if='schoolItemNew.length>0'>
        <div class="school-item-body" :class='item.active?"school-active":""' v-for="(item,index) in schoolItemNew"
          @click="activeScohool(index)"><span>{{item.label}}</span></div>
      </template>
      <template v-else>
        <div class="school-item-body"><span>无此幼儿园</span></div>
      </template>

    </div>


    <!--<div id="vconso">
			vconsole
		</div>-->
  </div>
</template>

<script>
  import {
    JSEncrypt
  } from 'jsencrypt';
  import {
    Indicator,
    Toast
  } from 'mint-ui';
  export default {
    name: 'Login',
    data() {
      return {
        User: '',
        Pwd: '',
        Href: '',
        Sid: '',
        schoolItem: [],
        schoolItemNew: [],
        schoolShow: false,
        value: ''

      }
    },
    components: {
      JSEncrypt
    },
    created: function () {
      // localStorage.clear()
      if (localStorage.getItem('sessionId') != null) {
        this.$router.push({
          path: `/Index`
        })
      }
      /*芯仔强行要求放着，不允许删除
      	this.Href = location.href + this.Sid;
      			let encryptor = new JSEncrypt();
      			encryptor.setPublicKey(`-----BEGIN PUBLIC KEY-----
      MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuD61FMCPRck8p/VgJrPMrRJJE
      r6jGHiudyrCHp4jh+qHHaqKGx9em3nYj/fkg7nvnQJMbnRvJ4qwvAMGIzIz5xKjX
      b+g/qnhKAEhmK3oEHtfE235HoYBLjUP+ihZO2XX2wFn2eMWajrTSoKN5Lh+3K2pV
      0FEtPkdE34dlyrIJZwIDAQAB
      -----END PUBLIC KEY-----`)
      			let rsaPassWord = encryptor.encrypt('嘿');
      */
      this.getSchool();
    },
    methods: {
      GotoForgetPwd: function (e) {
        //跳转到忘记密码
        this.$router.push({
          path: `/ForgetPwd`
        })
      },
      GotoInitialPwd: function (e) {
        //跳转到忘记密码
        this.$router.push({
          path: `/InitialPwd`
        })
      },
      login: function (e) {
        if (this.User == '') {
          Toast({
            message: '请输入用户名',
            duration: 1000
          });
        } else {
          if (this.Pwd == '') {
            Toast({
              message: '请输入密码',
              duration: 1000
            });
          } else {
            if (this.Sid == '') {
              Toast({
                message: '请选择幼儿园',
                duration: 1000
              });
            } else {
              this.loginFun();
            }
          }
        }
      },
      ClearUser: function () {
        this.User = ''
      },
      activeScohool(e) {
        this.Sid = this.schoolItemNew[e].value;
        this.value = this.schoolItemNew[e].label;
        this.schoolItemNew[e].check = true;
        this.schoolShow = false;
      },
      // 回味
      userBlur() {
        document.body.scrollTop = 0;
      },
      pwdBlur() {
        document.body.scrollTop = 0;
      },
      getSchool() {
        var vm = this;
        this.$ApiCom.Select().then(function (res) {
          res.data.data.forEach(function (v) {
            vm.schoolItem.push({
              value: v.sid,
              label: v.name,
              check: false
            })
            vm.schoolItemNew.push({
              value: v.sid,
              label: v.name,
              check: false
            })
          })

        })
      },
      schoolTab(e) {
        this.Sid = e
      },
      searSchool(e) {
        this.schoolShow = true;
      },
      schoolIpt(e) {
        var val = this.value;
        if (val == '') {
          this.schoolItemNew = this.schoolItem;
        } else {
          var vm = this;
          var iptVal = this.schoolItem;
          vm.schoolItemNew = [];
          this.schoolItem.forEach(function (v) {
            if (v.label.indexOf(val) != -1) {
              vm.schoolItemNew.push({
                value: v.sid,
                label: v.label,
                check: false
              })
            }
          })
        }
      },
      falseShow() {
        this.schoolShow = false;
      },
      loginFun() {
        if (this.User == '123' && this.Pwd == '123456') {
          //跳转登陆
          this.$router.push({
            path: `/Admin`
          })
        } else {
          console.log(this.Sid)
          var UriData = window.location.href.match(/(\S*)#/)[1];
          //回调地址
          //回调->确认信息
          var CheckPerfectInfo = encodeURIComponent(UriData + '#/CheckPerfectInfo/1');
          // 回调->设置支付密码
          var SetPayPwd = encodeURIComponent(UriData + '#/SetPayPwd');
          // 回调->首页
          var IndexInfo = encodeURIComponent(UriData + '#/Index');
          this.Href = encodeURIComponent(window.location.href);
          Indicator.open({
            text: '登陆中...',
          });
          var vm = this;
          this.$ApiCom.Login(this.User, this.Pwd, this.Href, this.Sid).then(function (res) {
            Indicator.close();
            if (res.data.errCode == 0) {
              if (res.data.data.confirm == 0) {
                // 尚未确认信息
                sessionStorage.setItem('sessionId', res.data.data.sessionId);
                location.href = 'http://xyb.wujiaping.cn/parent/login/getWxInfo?redirectUri=' + CheckPerfectInfo +
                  '&&sessionId=' + res.data.data.sessionId;
                 console.log('sessionId',res.data.data.sessionId)
              } else if (!res.data.data.verify_pass) {
                // 尚未设置密码
                sessionStorage.setItem('sessionId', res.data.data.sessionId);
                location.href = 'http://xyb.wujiaping.cn/parent/login/getWxInfo?redirectUri=' + SetPayPwd +
                  '&&sessionId=' + res.data.data.sessionId;
              } else {
                // 登陆首页  
                localStorage.setItem('sessionId', res.data.data.sessionId);
                location.href = 'http://xyb.wujiaping.cn/parent/login/getWxInfo?redirectUri=' + IndexInfo +
                  '&&sessionId=' + res.data.data.sessionId;
              }
            } else {
              //响应错误
              Toast({
                message: res.data.errMsg,
                duration: 1000
              });
            }
          }, function (err) {
            //   响应超时
            Indicator.close();
            Toast({
              message: '响应超时',
              duration: 1000
            });
          })
        }
      }
    },


  }

</script>
<style scoped>
  @import url("css/Login.css");

</style>
